﻿using System.Windows.Controls;

namespace Red.Controls
{
    /// <summary>
    /// Represents a grid view that displays data in auto sized columns (column's width is based on the column's content)    
    /// </summary>
    public class AutoSizedGridView : GridView
    {
        protected override void PrepareItem( ListViewItem item )
        {
            foreach ( GridViewColumn column in Columns )
            {
                // Setting NaN for the column width automatically determines the required width enough to hold the content completely.
                // If column width was set to NaN already, set it to ActualWidth temporarily and then to NaN. This raises the property change event and recomputes the width.
                if ( double.IsNaN( column.Width ) )
                    column.Width = column.ActualWidth;
                column.Width = double.NaN;
            }
            base.PrepareItem( item );
        }
    }
}
